package com.zhkj.txg.module.mine.ui

import android.app.Activity
import android.content.Intent
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.zhkj.lib.base.BaseActivity
import com.zhkj.txg.R
import kotlinx.android.synthetic.main.activity_coupon.*

class CouponActivity : BaseActivity() {

    companion object {
        private val TYPE_ALL = 0
        private val TYPE_HISTORY = 1
        private val TYPE = "type"

        fun all(activity: Activity) {
            val intent = Intent(activity, CouponActivity::class.java)
                .putExtra(TYPE, TYPE_ALL)
            activity.startActivity(intent)
        }

        fun history(activity: Activity) {
            val intent = Intent(activity, CouponActivity::class.java)
                .putExtra(TYPE, TYPE_HISTORY)
            activity.startActivity(intent)
        }
    }

    override fun statusTitleBar(): View? = titleCoupon
    override val layoutResId: Int = R.layout.activity_coupon

    private val allFragment: CouponListFragment by lazy {
        CouponListFragment.newInstance(0, 0, 0)
    }
    private val typeAllFragment: CouponListFragment by lazy {
        CouponListFragment.newInstance(1, 0, 0)
    }
    private val typeGoodsFragment: CouponListFragment by lazy {
        CouponListFragment.newInstance(2, 0, 0)
    }
    private val typeShopsFragment: CouponListFragment by lazy {
        CouponListFragment.newInstance(3, 0, 0)
    }
    private val typeUsedFragment: CouponListFragment by lazy {
        CouponListFragment.newInstance(0, 1, 1)
    }
    private val typeExpiredFragment: CouponListFragment by lazy {
        CouponListFragment.newInstance(0, 1, 2)
    }

    override fun initUI() {
        with(vpCoupon) {
            adapter = object : FragmentStateAdapter(this@CouponActivity) {
                override fun getItemCount(): Int = if (intent.getIntExtra(TYPE, TYPE_ALL) == TYPE_ALL) 4 else 2
                override fun createFragment(position: Int): Fragment = getCurrentFragment(position)
            }
            getChildAt(0)?.overScrollMode = View.OVER_SCROLL_NEVER
        }
        tvCouponHistory.setOnClickListener {
            history(this)
        }
        TabLayoutMediator(
            tabCoupon, vpCoupon,
            TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                when (position) {
                    1 -> tab.customView =
                        layoutInflater.inflate(R.layout.item_collect_shop, null).apply {
                            findViewById<TextView>(R.id.tvTab).text =
                                if (intent.getIntExtra(TYPE, TYPE_ALL) == TYPE_ALL) getString(R.string.coupon_type_all)
                                else getString(R.string.coupon_type_expired)
                        }
                    2 -> tab.customView =
                        layoutInflater.inflate(R.layout.item_collect_shop, null).apply {
                            findViewById<TextView>(R.id.tvTab).text = getString(R.string.coupon_type_goods)
                        }
                    3 -> tab.customView =
                        layoutInflater.inflate(R.layout.item_collect_shop, null).apply {
                            findViewById<TextView>(R.id.tvTab).text =
                                getString(R.string.coupon_type_shops)
                        }
                    else -> tab.customView =
                        layoutInflater.inflate(R.layout.item_collect_shop, null).apply {
                            findViewById<TextView>(R.id.tvTab).text =
                                if (intent.getIntExtra(TYPE, TYPE_ALL) == TYPE_ALL) getString(R.string.all)
                                else getString(R.string.coupon_type_used)
                        }
                }
            }).attach()

        // 判断是全部优惠券还是历史优惠券
        val type = intent.getIntExtra(TYPE, TYPE_ALL)
        if (type == TYPE_HISTORY) {
            tvCouponHistory.visibility = View.GONE
            (tabCoupon.layoutParams as FrameLayout.LayoutParams).gravity = Gravity.START
        }
    }

    private fun getCurrentFragment(position: Int): CouponListFragment = when (position) {
        1 -> if (intent.getIntExtra(TYPE, TYPE_ALL) == TYPE_ALL) typeAllFragment else typeExpiredFragment
        2 -> typeGoodsFragment
        3 -> typeShopsFragment
        else -> if (intent.getIntExtra(TYPE, TYPE_ALL) == TYPE_ALL) allFragment else typeUsedFragment
    }
}